package yy.robot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import yy.robot.domain.Paging;
import yy.robot.domain.Strategy;
import yy.robot.service.StrategyService;

/**
 * Created by BaiJiFeiLong@gmail.com at 2018/5/3 11:13
 */
@RestController
@RequestMapping("/strategies")
public class StrategyController {

    private final StrategyService strategyService;

    @Autowired
    public StrategyController(StrategyService strategyService) {
        this.strategyService = strategyService;
    }

    @GetMapping
    public Paging<Strategy> index(
            @RequestParam(required = false, defaultValue = "0") int page,
            @RequestParam(required = false, defaultValue = "10") int size
    ) {
        Page<Strategy> strategyPage = strategyService.findAll(PageRequest.of(page, size));
        return Paging.fromPage(strategyPage);
    }

    @PostMapping
    public Strategy create(
            Strategy strategy
    ) {
        return strategyService.create(strategy);
    }

    @GetMapping("{id}")
    public Strategy show(@PathVariable long id) {
        return strategyService.findById(id);
    }

    @PatchMapping("{id}")
    public Strategy update(
            @PathVariable long id,
            Strategy strategy
    ) {
        strategy.setId(id);
        return strategyService.update(strategy);
    }

    @DeleteMapping("{id}")
    public void delete(@PathVariable long id) {
        strategyService.destroy(id);
    }
}
